class Solution {
public:
    int maxEvents(vector<vector<int>>& events) {
        sort(events.begin(), events.end(), [](const auto& e1, const auto& e2){
            return e1[0] < e2[0];
        });

        priority_queue<int, vector<int>, greater<int>> qu;

        int n = events.size();
        int maxday = 0, minday = events[0][0];
        for(int i = 0; i < n; ++i)
            maxday = max(maxday, events[i][1]);

        int ans = 0;
        for(int i = minday, k = 0; i <= maxday; ++i)
        {
            while(k < n && events[k][0] == i)
                qu.push(events[k++][1]);
            
            while(!qu.empty() && qu.top() < i)
                qu.pop();

            if(!qu.empty())
            {
                ++ans;
                qu.pop();
            }
        }        
        return ans;
    }
};
